var mongoose = require("../modules/database");
var Schema = mongoose.Schema;
var schema = new Schema({
    teacherId: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'teachers'
    },
    classStatus: {
        type: String,
        default: null,
    },
    className: {
        type: String,
        default: null,
    },
    personCount: {
        type: String,
        default: null,
    },
    chargeTeacher: {
        type: String,
        default: null,
    },
    teachTeacher: {
        type: String,
        default: null,
    },
    jobTeacher: {
        type: String,
        default: null,
    },
    assistant: {
        type: String,
        default: null,
    },
    classAddress: {
        type: String,
        default: null,
    },
    courseName: {
        type: String,
        default: null,
    },
    startDate: {
        type: String,
        default: null,
    },
    endDate: {
        type: String,
        default: null,
    },
    officialDate: {
        type: String,
        default: null,
    },
    createTime: {
        type: Date,
        default: Date.now
    }
})

schema.statics = ({
    addclass: function (data, cb) {
        var filter = { className: data.className }
        Model.findOne().or(filter).exec(function (err, result) {
            if (err) {
                return cb({ code: 201, message: err });
            }
            if (result) {
                return cb({ code: 201, message: "注册的班级已存在" });
            } else {
                Model.findOne().or(filter).exec(function (err, result) {
                    if (err) {
                        return cb({ code: 201, message: err });
                    }
                    if (result) {
                        return cb({ code: 201, message: "注册的班级已存在" });
                    } else {
                        Model.insertMany(data, function (err, result) {
                            if (err) {
                                return cb({ code: 201, message: err });
                            }
                            return cb(null, result);
                        })
                    }
                })
            }
        })
    }
})
var Model = mongoose.model("classes", schema);
module.exports = Model;